package com.xiaobaibai.controller;

import com.xiaobaibai.common.response.ResponseFormat;
import com.xiaobaibai.pojo.Comment;
import com.xiaobaibai.pojo.CommentChildren;
import com.xiaobaibai.server.comment.ICommentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class CommentController {

    @Autowired
    ICommentService commentService;

    private static final String QQIMAGE="http://image.xiaobaibai.ink/myQQimage.jpg";

    private static final String NAME="忆凡";

    @GetMapping("/getCommentList")
    public ResponseFormat getCommentList(){
        List<Comment> commentList = commentService.getCommentList();
        return ResponseFormat.createSuccessResponse("留言读取成功",commentList);
    }

    @PostMapping("/delComment")
    public ResponseFormat delComment(@Param("id")Integer id){
        commentService.delComment(id);
        return ResponseFormat.createSuccessResponseOnlyMessage("删除成功");
    }

    @PostMapping("/delCommentChildren")
    public ResponseFormat delCommentChildren(@Param("id")Integer id){
        commentService.delCommentChildren(id);
        return ResponseFormat.createSuccessResponseOnlyMessage("删除成功");
    }

    @PostMapping("/addCommentChildren")
    public ResponseFormat addCommentChildren(@RequestBody Map<String,Object> data){
        //只能添加子评论,因为只能回复
        CommentChildren commentChildren=new CommentChildren();
        //这个flag的作用是什么呢?就是说,如果我点击回复点击到的是父评论,那么这里为false,在显示时就不会用xx回复xx,只有一个xx
        Integer pId= (Integer) data.get("pId");
        boolean flag= (boolean) data.get("flag");//仅在用户端有用,管理端默认xx回复xx
        String content= (String) data.get("content");
        String pName=(String) data.get("pName");
        commentChildren.setPId(pId);
        commentChildren.setPName(pName);
        commentChildren.setName(NAME);
        commentChildren.setFlag(flag);
        commentChildren.setContent(content);
        commentChildren.setImg(QQIMAGE);
        commentChildren.setCareteTime(new Date());
        commentService.addCommentChildren(commentChildren);
        return ResponseFormat.createSuccessResponse("success",commentChildren);
    }

}
